package com.sugar.real.estate.module.business.controller.admin.dicttype;

import com.sugar.real.estate.framework.apilog.core.annotation.ApiAccessLog;
import com.sugar.real.estate.framework.common.pojo.CommonResult;
import com.sugar.real.estate.framework.common.pojo.PageParam;
import com.sugar.real.estate.framework.common.pojo.PageResult;
import com.sugar.real.estate.framework.common.util.object.BeanUtils;
import com.sugar.real.estate.framework.excel.core.util.ExcelUtils;
import com.sugar.real.estate.module.business.controller.admin.dicttype.vo.BizDictDataSimpleRespVO;
import com.sugar.real.estate.module.business.controller.admin.dicttype.vo.BizDictTypePageReqVO;
import com.sugar.real.estate.module.business.controller.admin.dicttype.vo.BizDictTypeRespVO;
import com.sugar.real.estate.module.business.controller.admin.dicttype.vo.BizDictTypeSaveReqVO;
import com.sugar.real.estate.module.business.dal.dataobject.dicttype.BizDictDataDO;
import com.sugar.real.estate.module.business.dal.dataobject.dicttype.BizDictTypeDO;
import com.sugar.real.estate.module.business.service.dicttype.BizDictTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.sugar.real.estate.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.sugar.real.estate.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 字典类型")
@RestController
@RequestMapping("/business/biz-dict-type")
@Validated
public class BizDictTypeController {

    @Resource
    private BizDictTypeService bizDictTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建字典类型")
    @PreAuthorize("@ss.hasPermission('business:biz-dict-type:create')")
    public CommonResult<Long> createBizDictType(@Valid @RequestBody BizDictTypeSaveReqVO createReqVO) {
        return success(bizDictTypeService.createBizDictType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新字典类型")
    @PreAuthorize("@ss.hasPermission('business:biz-dict-type:update')")
    public CommonResult<Boolean> updateBizDictType(@Valid @RequestBody BizDictTypeSaveReqVO updateReqVO) {
        bizDictTypeService.updateBizDictType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除字典类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:biz-dict-type:delete')")
    public CommonResult<Boolean> deleteBizDictType(@RequestParam("id") Long id) {
        bizDictTypeService.deleteBizDictType(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除字典类型")
                @PreAuthorize("@ss.hasPermission('business:biz-dict-type:delete')")
    public CommonResult<Boolean> deleteBizDictTypeList(@RequestParam("ids") List<Long> ids) {
        bizDictTypeService.deleteBizDictTypeListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得字典类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:biz-dict-type:query')")
    public CommonResult<BizDictTypeRespVO> getBizDictType(@RequestParam("id") Long id) {
        BizDictTypeDO bizDictType = bizDictTypeService.getBizDictType(id);
        return success(BeanUtils.toBean(bizDictType, BizDictTypeRespVO.class));
    }

    @GetMapping("/get/project")
    @Operation(summary = "获得项目进度字典类型")
    @PreAuthorize("@ss.hasPermission('business:biz-dict-type:query')")
    public CommonResult<List<BizDictDataDO>> getBizDictTypeProject() {
        return success(bizDictTypeService.getBizDictTypeProject());
    }

    @GetMapping("/page")
    @Operation(summary = "获得字典类型分页")
    @PreAuthorize("@ss.hasPermission('business:biz-dict-type:query')")
    public CommonResult<PageResult<BizDictTypeRespVO>> getBizDictTypePage(@Valid BizDictTypePageReqVO pageReqVO) {
        PageResult<BizDictTypeDO> pageResult = bizDictTypeService.getBizDictTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BizDictTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出字典类型 Excel")
    @PreAuthorize("@ss.hasPermission('business:biz-dict-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBizDictTypeExcel(@Valid BizDictTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BizDictTypeDO> list = bizDictTypeService.getBizDictTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "字典类型.xls", "数据", BizDictTypeRespVO.class,
                        BeanUtils.toBean(list, BizDictTypeRespVO.class));
    }

    @GetMapping("/data/{typeId}")
    public CommonResult<List<BizDictDataSimpleRespVO>> getAllDictDataByTypeId(@PathVariable("typeId") Long typeId) {
        return success(bizDictTypeService.getAllDictDataByTypeId(typeId));
    }

    // ==================== 子表（字典数据） ====================

    @GetMapping("/biz-dict-data/page")
    @Operation(summary = "获得字典数据分页")
    @Parameter(name = "dictTypeId", description = "字典类型")
    @PreAuthorize("@ss.hasPermission('business:biz-dict-type:query')")
    public CommonResult<PageResult<BizDictDataDO>> getBizDictDataPage(PageParam pageReqVO,
                                                                      @RequestParam("dictTypeId") Long dictTypeId) {
        return success(bizDictTypeService.getBizDictDataPage(pageReqVO, dictTypeId));
    }

    @PostMapping("/biz-dict-data/create")
    @Operation(summary = "创建字典数据")
    @PreAuthorize("@ss.hasPermission('business:biz-dict-type:create')")
    public CommonResult<Long> createBizDictData(@Valid @RequestBody BizDictDataDO bizDictData) {
        return success(bizDictTypeService.createBizDictData(bizDictData));
    }

    @PutMapping("/biz-dict-data/update")
    @Operation(summary = "更新字典数据")
    @PreAuthorize("@ss.hasPermission('business:biz-dict-type:update')")
    public CommonResult<Boolean> updateBizDictData(@Valid @RequestBody BizDictDataDO bizDictData) {
        bizDictTypeService.updateBizDictData(bizDictData);
        return success(true);
    }

    @DeleteMapping("/biz-dict-data/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除字典数据")
    @PreAuthorize("@ss.hasPermission('business:biz-dict-type:delete')")
    public CommonResult<Boolean> deleteBizDictData(@RequestParam("id") Long id) {
        bizDictTypeService.deleteBizDictData(id);
        return success(true);
    }

    @DeleteMapping("/biz-dict-data/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除字典数据")
    @PreAuthorize("@ss.hasPermission('business:biz-dict-type:delete')")
    public CommonResult<Boolean> deleteBizDictDataList(@RequestParam("ids") List<Long> ids) {
        bizDictTypeService.deleteBizDictDataListByIds(ids);
        return success(true);
    }

	@GetMapping("/biz-dict-data/get")
	@Operation(summary = "获得字典数据")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:biz-dict-type:query')")
	public CommonResult<BizDictDataDO> getBizDictData(@RequestParam("id") Long id) {
	    return success(bizDictTypeService.getBizDictData(id));
	}

}